/*  This file is part of YATracker.
    YATracker is free software: you can redistribute it and/or modify it under the terms
of the GNU Lesser General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
    YATracker is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General Public License along with
YATracker. If not, see <http://www.gnu.org/licenses/>. */

#ifndef QGPSSOURCE_H
#define QGPSSOURCE_H

#include <QObject>
#include <QFile>
#include <QTimer>

enum GpsStatus
{
    GpsStatusNone       = 0,    // No status / not started
    GpsStatusConnect    = 1,    // Connected to the GPS receiver
    GpsStatusSignal     = 2,    // Connected and receiving the data
    GpsStatusError      = 3,    // Error state
};

enum GpsReadState
{
    GpsReadStateNone        = 0,
    GpsReadStateWaitStart   = 1,
    GpsReadStateWaitEnd     = 2,
    GpsReadStateError       = 3,
};

class QGpsSource : public QObject
{
Q_OBJECT
public:
    explicit    QGpsSource();
               ~QGpsSource();

    void        setComPort(QString portName);
    bool        gpsConnect();
    bool        isConnected() const { return m_status != GpsStatusNone; }
    void        gpsClose();
    GpsStatus   getStatus();

signals:
    void        gpsMessageRMC(double latitude, double longitude);  // Location based on RMC message
    void        gpsMessageGSA(int fix, float pdop, float hdop, float vdop, int fixSats);  // Fix and DOP info based on GSA message
    void        gpsMessageGGA(int fix, double latitude, double longitude, int fixSats);  // Fix and location based on GGA message

private slots:
    void        portReadTimerTick();

private:
    void        parsePortBuffer();  // Continue parsing using data in the buffer
    void        parseMessage(QString message);  // Parse an NMEA message placed at the start of the m_portBuffer

private:
    GpsStatus   m_status;
    QString     m_portName;
    void*       m_portHandle;
    QTimer      m_portReadTimer;
    GpsReadState m_portReadState;
    char        m_portBuffer[1024];
    qint32      m_portBufferUsed;   // Number of bytes used in the m_portBuffer

};

#endif // QGPSSOURCE_H
